﻿/*
// +----------------------------------------------------------------------------+
// |                                                                            |
// |  Copyright (1999-2010) by Actify, Inc.                                     |
// |                                                                            |
// |  This program is proprietary with Actify, Inc., and is not to              |
// |  be reproduced or transmitted in any form or by any means or used in any   |
// |  manner except as authorized in writing by Actify, Inc.                    |
// |                                                                            |
// +----------------------------------------------------------------------------+
// |                                                                            |
// |   File Name : ScriptableManagedType.cs                                     |
// |  Written By : Clay Tornquist                                               |
// |        Date : Feb 09 2010                                                  |
// | Description :                                                              |
// |                                                                            |
// +----------------------------------------------------------------------------+
// |                                                                            |
// | Notes:                                                                     |
// |                                                                            |
// +----------------------------------------------------------------------------+

	Changes:
		[2010-02-09 CMT] - Initial implementation.

*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace SpinFireSL
{
	/// <summary>
	/// HTML script callable interface object
	/// </summary>
	public class ScriptableManagedType
	{
		#region Attributes

		private MainPage _page;
		private Stack<TreeItem> _treeList = new Stack<TreeItem>();
		private ResourceHelper _rh = new ResourceHelper();

		#endregion

		#region Constructor

		/// <summary>
		/// Created and registerd by the Main page so that the HTML events can callback to the Silverlight
		/// code. VB compliant methods can be called directly from Silverlight side.  Non-compliant methods 
		/// have to be called from the HTML side, so some of these methods are not for events but are to 
		/// support non-VB compliant methods.
		/// </summary>
		/// <param name="page">the main page that has registered this so the callback can effect changes to the main page</param>
		internal ScriptableManagedType(MainPage page)
		{
			_page = page;
		}

		#endregion

		#region Public Members

		/// <summary>
		/// Called when the control has completed initialization. The current implementation
		/// gives the main page access to the view browser control.  
		/// </summary>
		/// <param name="root"></param>
		[ScriptableMember]
		public void ControlInitialized(HtmlElement root)
		{
			_page.ViewBrowser = root;
			root.Invoke("PickingType", "MARKUP");
		}

		[ScriptableMember]
		public void LoadPartInfo(string url, string xml)
		{
			_page.LoadFile(url, xml);
		}

		/// <summary>
		/// Called when a model has been loaded into the control.  Current implementation gets the
		/// tree loaded with the model information.
		/// </summary>
		/// <param name="modelnum">the model number that ws loaded</param>
		[ScriptableMember]
		public void ImportModelCompleted(int modelnum)
		{
			ScriptObject rootnode = _page.ViewBrowser.Invoke("SceneGetRootNode") as ScriptObject;
			string name = _page.ViewBrowser.GetProperty("ModelName") as string;
			string str = rootnode.Invoke("GetAssemblyXML") as string;
			_page.xmldata = str;
			XDocument document = XDocument.Parse(str);
			_page.AddModel(modelnum, name, document);
			_page.ViewBrowser.Invoke("PickingType", "MARKUP");
			_page.SetRenderIcons();

			uint color = Convert.ToUInt32(_page.ViewBrowser.GetProperty("RGBBackgroundColor"));
			_page.SetBackgroundColor(color);
			_page.ViewBrowser.SetProperty("BackgroundSmooth", 0);
		}

		/// <summary>
		/// Called when a mouse action has occurred.
		/// </summary>
		/// <param name="action">the type of mouse action that has occurred</param>
		[ScriptableMember]
		public void OnMouseAction(string action)
		{
			string a = action;
			switch (action)
			{
				case "FOCUS_ON_POINT_COMPLETE":
				case "FOCUS_ON_POINT_ABORTED":
					_page.spinStoryBoard.Stop();
					_page.ViewBrowser.Invoke("PickingType", "MARKUP");
					break;

				case "FOCUS_ON_PART_COMPLETE":
				case "FOCUS_ON_PART_ABORTED":
					_page.spinStoryBoard.Stop();
					_page.ViewBrowser.Invoke("PickingType", "MARKUP");
					break;

				case "ZOOM_WINDOW_COMPLETE":
				case "ZOOM_WINDOW_ABORTED":
					_page.spinStoryBoard.Stop();
					_page.ViewBrowser.Invoke("PickingType", "MARKUP");
					break;
				case "ROTATE_MODE":
					_page.SetLeftMouseState(MainPage.MouseState.Rotate);
					break;
				case "PAN_MODE":
					_page.SetLeftMouseState(MainPage.MouseState.Pan);
					break;
				case "ZOOM_MODE":
					_page.SetLeftMouseState(MainPage.MouseState.Zoom);
					break;
				case "LEFT_MOUSE_BUTTON_UP":
					_page.ResetHighlight();
					break;
			}
			//System.Diagnostics.Debug.WriteLine(action);
		}

		/// <summary>
		/// Called when a markup has been added or modified
		/// </summary>
		/// <param name="addedMarkup">the markup that was added or modified</param>
		[ScriptableMember]
		public void MarkupAdded(ScriptObject addedMarkup)
		{
			_page.AddMarkup(addedMarkup);
			_page.ViewBrowser.Invoke("PickingType", "MARKUP");
		}

		/// <summary>
		/// Called when a markup has been deleted
		/// </summary>
		/// <param name="markup">the markup that was deleted</param>
		[ScriptableMember]
		public void OnMarkupDelete(ScriptObject markup)
		{
			_page.DeleteMarkup(markup);
			_page.ViewBrowser.Invoke("PickingType", "MARKUP");
		}

		/// <summary>
		/// Called when a markup has been picked
		/// </summary>
		/// <param name="markup">that markup that was picked</param>
		[ScriptableMember]
		public void OnPickMarkup(ScriptObject markup)
		{
			_page.PickMarkup(markup);
		}

		/// <summary>
		/// Enumerating through the markups is non VB compliant so this allows the HTML to call the 
		/// methods and return the response to silverlight.
		/// </summary>
		/// <param name="markup">the markup that was found</param>
		[ScriptableMember]
		public void SceneMarkup(ScriptObject markup)
		{
			_page.MarkupLoad = markup;
		}

		[ScriptableMember]
		public void ShadedMode(short mode)
		{
			_page.SetRenderIcons(mode);
		}

		#endregion

	}
}
